package com.example.scope;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


/**
 * 单例bean中注入多例bean失效问题
 */
public class Example {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(ScopeConfig.class);
        SingletonInstance s1 = applicationContext.getBean(SingletonInstance.class);
        SingletonInstance s2 = applicationContext.getBean(SingletonInstance.class);
        System.out.println("s1 == s2: " + (s1 == s2)); // singleton scope

        PrototypeInstance p1 = applicationContext.getBean(PrototypeInstance.class);
        PrototypeInstance p2 = applicationContext.getBean(PrototypeInstance.class);
        System.out.println("p1 == p2: " + (p1 == p2)); // prototype scope

        System.out.println(s1.getInstance());
        System.out.println(s1.getInstance());
        System.out.println(s2.getInstance());
        System.out.println(s2.getInstance());
    }
}
